from aip import AipNlp
import pandas as pd
import numpy as np
import time

# 此处输入baiduAIid
APP_ID = '25681745'
API_KEY = 'q8qgIHHp175YGjXINhbx2sAZ'
SECRET_KEY = '5vsl618YQsGUSuFZ3ZGDPFmgsLvraPAU'

client = AipNlp(APP_ID, API_KEY, SECRET_KEY)


def isPostive(text):
    try:
        if client.sentimentClassify(text)['items'][0]['sentiment'] == 1:
            return "中性"
        else:
            if client.sentimentClassify(text)['items'][0]['positive_prob'] > 0.5:
                return "积极"
            else:
                return "消极"
    except:
        return "积极"


# 读取文件，注意修改文件路径
file_path = '../data/test01.xls'
data = pd.read_excel(file_path)

moods = []
count = 1
for i in data['微博内容']:
    moods.append(isPostive(i))
    count += 1
    print("目前分析到：" + str(count) + isPostive(i))

data['情感倾向'] = pd.Series(moods)

# 此处为覆盖保存
data.to_excel(file_path)
print("分析完成，已保存")

